﻿using DDDDemo2.DALs;

namespace DDDDemo2;

public class AccountRepository : IAccountRepository
{
    private readonly AccountDAL _AccountDAL;
    private readonly IObjectMapper _ObjectMapper;

    public AccountRepository(AccountDAL accountDAL, IObjectMapper objectMapper)
    {
        _AccountDAL = accountDAL;
        _ObjectMapper = objectMapper;
    }

    public async Task<Account> FindAsync(Guid id)
    {
        var data = await _AccountDAL.GetAccountByIdAsync(id);
        var result = new Account(data.UserId, data.AccountNo, data.Balance);
        return result;
    }

    public async Task<Account> FindAsync(string accountNo)
    {
        var data = await _AccountDAL.GetAccountByNoAsync(accountNo);
        var result = new Account(data.UserId, data.AccountNo, data.Balance);
        return result;
    }

    public async Task UpdateAsync(Account account)
    {
        var entity = _ObjectMapper.MapTo<AccountDALModel>(account);
        await _AccountDAL.UpdateAsync(entity);
    }
}